# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/27 下午5:02
 @Author  : tgy
 
"""
import os
import re
from collections.abc import Iterable

def test01(path):
    ...
    for path, dir_list, file_list in os.walk(path, topdown=True):
        ...
        dir_list[:] = [dir_name for dir_name in dir_list if 'python-base' not in dir_name]
        # for file in file_list:
        #     yield os.path.join(path,file)
        yield path


"""
    1.路径查找，并过滤
    2.打开文件
    3.读取文件内容
    4.过滤文件内容,返回过滤的内容
"""

import fnmatch

def find_path(base_path, pattern):
    ...
    for path, dirs, files in os.walk(base_path):
        ...
        for file in files:
            ...
            if fnmatch.fnmatch(file, pattern):
                yield os.path.join(path, file)


def open_file(paths):
    ...
    for path in paths:
        f = open(path,mode='rt',encoding='utf-8')
        yield f
        f.close()



def concatenate(files):
    # iter(file) -> 迭代对象
    # next(file) -> 获取 f
    for file in files:
        yield from file


def gen_grep(pattern, lines):
    ...
    pat = re.compile(pattern)

    for line in lines:
        if pat.search(line):
            yield line


def test02():
    ...
    paths = find_path('./', "*.py")
    files = open_file(paths)
    lines = concatenate(files)
    for line in gen_grep('end',lines):
        print(line)


if __name__ == '__main__':
    ...
    # for path in test01('/Users/apple/Documents/python/python-guide'):
    #     print(path)
    # nums = ['abc','12','tgy']
    # for c in concatenate(nums):
    #     print(c)
    # test02()
    l = [1, 2, 3]
    print(isinstance(l,Iterable))